package com.ch;

import java.util.*;

/**
 * @author chenhong
 * @version 1.0.0
 * @ClassName 同构字符串.java
 * @Description TODO
 * @createTime 2020年12月27日 10:59:00
 */
public class 同构字符串 {
}


//哈希表
class Solution205 {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() <= 1) {
            return true;
        }
        HashMap<Character, Character> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                if (t.charAt(i) != map.get(s.charAt(i))) {
                    return false;
                }
            } else if (map.containsValue(t.charAt(i))) {
                return false;
            } else {
                map.put(s.charAt(i), t.charAt(i));
            }
        }
        return true;
    }
}

//哈希表，双向映射
class Solution205_2 {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() <= 1) {
            return true;
        }
        HashMap<Character, Character> s2t = new HashMap<>();
        HashMap<Character, Character> t2s = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            char x = s.charAt(i), y = t.charAt(i);
            if ((s2t.containsKey(x) && s2t.get(x) != y) || (t2s.containsKey(y) && t2s.get(y) != x)) {
                return false;
            }
            s2t.put(x, y);
            t2s.put(y, x);
        }
        return true;
    }
}


